package com.book;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.book.dao.TbBookDao;
import com.book.dao.TbTypeDao;
import com.book.entity.TbBook;
import com.book.entity.TbType;
import lombok.RequiredArgsConstructor;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName:Book
 * author:xiaoshun
 * time:2024/11/8 下午3:51
 * description:
 */
@SpringBootTest
public class text {
    @Resource
    private TbTypeDao tbTypeDao;
    @Resource
    private TbBookDao tbBookDao;
@Test
    public void test() {
        Map<String, Object> param = new HashMap<>();
        param.put("key", "49a4fc76a587bd140f435b05af3b79d8");
        //执行对应的请求 获取响应
        String json = HttpUtil.post("http://apis.juhe.cn/goodbook/catalog", param);
        List<TbType> questionList = new ArrayList<>();
        //校验 接口是否返回成功
        if (StringUtils.hasLength(json)) {
            //验证本次请求响应是否成功
            JSONObject jo1 = new JSONObject(json);
            if ("success".equals(jo1.getStr("reason"))) {
                //返回成功
                //获取对应的测试题
                JSONArray ja = jo1.getJSONArray("result");
                for (int i = 0; i < ja.size(); i++) {
                    JSONObject jo2 = ja.getJSONObject(i);
                    TbType tbType = new TbType(Integer.parseInt(jo2.getStr("id")), jo2.getStr("catalog"));

                    questionList.add(tbType);
                }
            }
        }
        tbTypeDao.insert(questionList);
    }

@Test
    public void testBook() {
        Map<String, Object> param = new HashMap<>();
        param.put("key", "49a4fc76a587bd140f435b05af3b79d8");
        param.put("catalog_id","258");
        param.put("pn","1");
        param.put("rn","10");
        //执行对应的请求 获取响应
        String json = HttpUtil.post("http://apis.juhe.cn/goodbook/query", param);
        System.out.println(json);
        List<TbBook> books = new ArrayList<>();
        if (StringUtils.hasLength(json)){
            JSONObject jo1 = new JSONObject(json);
            System.out.println("jo1"+jo1);
            if ("Success".equals(jo1.getStr("reason"))){
                JSONObject result = jo1.getJSONObject("result");
                System.out.println("result"+result);
                JSONArray joa = result.getJSONArray("data");
                System.out.println("joa"+joa);
                for (int i = 0; i < joa.size(); i++) {
                    JSONObject jo2 = joa.getJSONObject(i);
                    LambdaQueryWrapper<TbBook> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(TbBook::getBookName,jo2.getStr("title"));
                    TbBook tbBook = tbBookDao.selectOne(wrapper);
                    if (tbBook!=null){
                        return;
                    }
                    TbBook q = new TbBook(jo2.getStr("title"),jo2.getStr("catalog"),jo2.getStr("tags"),jo2.getStr("sub1"),jo2.getStr("sub2"),
                            jo2.getStr("img"),jo2.getStr("reding"),jo2.getStr("online"));
                    books.add(q);
                }
            }
        }
        System.out.println(books);
        tbBookDao.insert(books);
    }
}
